/*
 * Copyright 2003-2005 (c) Live Media Pty Ltd. <argot@einet.com.au> 
 *
 * This software is licensed under the Argot Public License 
 * which may be found in the file LICENSE distributed 
 * with this software.
 *
 * More information about this license can be found at
 * http://www.einet.com.au/License
 * 
 * The Developer of this software is Live Media Pty Ltd,
 * PO Box 4591, Melbourne 3001, Australia.  The license is subject 
 * to the law of Victoria, Australia, and subject to exclusive 
 * jurisdiction of the Victorian courts.
 */

#include "TypeMeta.h"

#include "MetaEnvelop.h"

#include "../util/chunkbuffer.h"
#include "../common/BigEndianUnsignedByte.h"
#include "../common/BigEndianUnsignedShort.h"

type_name meta_envelop_name = { "meta.envelop" };

int meta_envelop_bind( type_element * element, type_definition * definition )
{
	meta_envelop * ma = (meta_envelop *) element;
	int rc;
	
	rc = type_object_bind( &element->object, definition->library, &meta_envelop_name );
	if ( rc != TYPE_OK ) return rc;

	rc = type_element_bind( element, definition );
	if ( rc != TYPE_OK ) return rc;
	
	rc = ma->size->element.bind( &(ma->size->element), definition );
	if ( rc != TYPE_OK ) return rc;
	
	rc = ma->type->element.bind( &(ma->type->element), definition );
	if ( rc != TYPE_OK ) return rc;
	
	return TYPE_OK;
}

void meta_envelop_init( meta_envelop * ma, meta_expression * size, meta_expression * type )
{
	type_element_init( &(ma->expression.element), &meta_envelop_bind );
	meta_expression_init( &(ma->expression), meta_envelop_do_read, meta_envelop_do_write );

	ma->size = size;
	ma->type = type;
}

void * meta_envelop_construct( type_list * list, type_map * map )
{
	type_item * item = type_list_head( list );
	meta_expression * size = (meta_expression *) type_item_get_ptr( item );	
	item = type_item_next( item );
	meta_expression * type = (meta_expression *) type_item_get_ptr( item );
	
	meta_envelop * ma = (meta_envelop*) type_malloc( sizeof( meta_envelop ) );
	meta_envelop_init( ma, size, type );
	return ma;
}

int meta_envelop_read( type_input_stream * in, type_element * element )
{
	TRACE( "meta_envelop_read" );
	
	return type_reader_auto( in, element, meta_envelop_construct );
}

int meta_envelop_write( type_output_stream * out, void * o, type_element * element )
{
	meta_envelop * ma = (meta_envelop *) o;
	
	TRACE( "meta_envelop_write" );
		
	type_output_write_named_object( out, &meta_expression_name, ma->size );
	type_output_write_named_object( out, &meta_expression_name, ma->type );
	return TYPE_OK;	
}

int meta_envelop_do_read( type_input_stream * in, type_element * element )
{
	meta_envelop * ma = (meta_envelop *) element;
	type_stack * stack = type_input_stack( in );
	type_list * list = type_stack_get_list( &in->stack );
	int rc;

	TRACE( "meta_envelop_do_read" );
		
	type_list_init( list );
	
	type_stack_push( stack );
	rc = ma->size->doRead( in, (type_element *) ma->size );
	if ( rc != TYPE_OK ) return rc;
	
	uint32_t size = 0;
	type_element * size_type = type_stack_type( stack );
	type_name * element_name = type_element_name( size_type );
	if ( type_name_equal( element_name, &uint8_name ) )
	{
		size = (uint32_t) type_stack_get_u8( stack );
	}
	else if ( type_name_equal( element_name, &uint16_name ) )
	{
		size = (uint32_t) type_stack_get_u16( stack );
	}
	else
	{
		type_input_set_error( in, BAD_ARRAY_SIZE );
		return TYPE_EOF;
	}
	type_stack_pop( stack );
	
	type_envelop * envelop = (type_envelop *) type_malloc( sizeof( type_envelop ) );
	envelop->data = type_malloc( size );
	envelop->size = size;
	envelop->definition = ma->type->element.definition;
	
	type_input_read_buffer( in, envelop->data, size );
	type_stack_set_ptr( type_input_stack(in), element, envelop );
	
	return TYPE_OK;
}

int meta_envelop_do_write( type_output_stream * out, void * o, type_element * element )
{
	meta_envelop * me = (meta_envelop *) element;
	chunk_buffer buffer;
	type_output_stream tmos;
	int rc;
	uint32_t size;
	
	TRACE( "meta_envelop_do_write" );
	
	chunk_buffer_init( &buffer );
	type_output_init( &tmos, out->map, &chunkBufferWriter, &buffer );
		
	rc = me->type->doWrite( &tmos, o, (type_element *) me->type );
	if ( rc != TYPE_OK )
	{
		chunk_buffer_clean( &buffer );
		type_output_destroy( &tmos );
		return rc;
	}

	type_output_destroy( &tmos );
	chunk_buffer_close( &buffer );
	
	size = chunk_buffer_length( &buffer );
	
	rc = me->size->doWrite( out, &size, (type_element *) me->size );
	if ( rc != TYPE_OK ) 
	{
		chunk_buffer_clean( &buffer );
		return rc;
	}
	
	rc = chunk_buffer_write_stream( &buffer, 0, size, out );
	if ( rc != size )
	{
		chunk_buffer_clean( &buffer );
		return TYPE_EOF;
	}
	
	chunk_buffer_clean( &buffer );
	return TYPE_OK;	
}


